package com.ruoyi.web.controller.gxs;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gxs.domain.GxsSecond;
import com.ruoyi.gxs.service.IGxsSecondService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 第二课堂得分Controller
 * 
 * @author 高晓松
 * @date 2023-12-27
 */
@RestController
@RequestMapping("/gxs/second")
public class GxsSecondController extends BaseController
{
    @Autowired
    private IGxsSecondService gxsSecondService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询第二课堂得分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:list')")
    @GetMapping("/list")
    public TableDataInfo list(GxsSecond gxsSecond)
    {
        startPage();
        List<GxsSecond> list = gxsSecondService.selectGxsSecondList(gxsSecond);
        return getDataTable(list);
    }

    /**
     * 导出第二课堂得分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:export')")
    @Log(title = "第二课堂得分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GxsSecond gxsSecond)
    {
        List<GxsSecond> list = gxsSecondService.selectGxsSecondList(gxsSecond);
        ExcelUtil<GxsSecond> util = new ExcelUtil<GxsSecond>(GxsSecond.class);
        util.exportExcel(response, list, "第二课堂得分数据");
    }

    /**
     * 获取第二课堂得分详细信息
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:query')")
    @GetMapping(value = "/{gxsSecondid}")
    public AjaxResult getInfo(@PathVariable("gxsSecondid") Long gxsSecondid)
    {
        return success(gxsSecondService.selectGxsSecondByGxsSecondid(gxsSecondid));
    }

    /**
     * 新增第二课堂得分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:add')")
    @Log(title = "第二课堂得分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GxsSecond gxsSecond)
    {
        //新增学生姓名、用户id、学号、部门id
        SysUser sysUser = sysUserService.selectUserById(getUserId());
        gxsSecond.setNickName(sysUser.getNickName());
        gxsSecond.setUserId(sysUser.getUserId());
        gxsSecond.setUserName(sysUser.getUserName());
        gxsSecond.setDeptId(sysUser.getDeptId());

        //创建者和创建时间
        gxsSecond.setCreateBy(sysUser.getUserName());
        gxsSecond.setCreateTime(new Date());
        return toAjax(gxsSecondService.insertGxsSecond(gxsSecond));
    }

    /**
     * 修改第二课堂得分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:edit')")
    @Log(title = "第二课堂得分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GxsSecond gxsSecond)
    {
        return toAjax(gxsSecondService.updateGxsSecond(gxsSecond));
    }

    /**
     * 删除第二课堂得分
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:remove')")
    @Log(title = "第二课堂得分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{gxsSecondids}")
    public AjaxResult remove(@PathVariable Long[] gxsSecondids)
    {
        return toAjax(gxsSecondService.deleteGxsSecondByGxsSecondids(gxsSecondids));
    }


    /**
     * 查询我的第二课堂得分列表
     */
//    @PreAuthorize("@ss.hasPermi('gxs:second:list')")
    @GetMapping("/MyList")
    public TableDataInfo MyList(GxsSecond gxsSecond)
    {
        startPage();
        String username = getUsername();
        gxsSecond.setUserName(username);
        List<GxsSecond> list = gxsSecondService.selectGxsSecondList(gxsSecond);
//        String username = getUsername();
//        for (int i = 0 ; i < list.size(); i++) {
//            if (!list.get(i).getUserName().equals(username) ) {
//                list.remove(i);
//                i--;
//            }
//        }

        return getDataTable(list);
    }

//    @GetMapping("/MyList")
//    public AjaxResult MyList()
//    {
//        Long userId = getUserId();
//
//        return success( gxsSecondService.selectGxsSecondByUserid(userId));
//    }
}
